<?php
namespace app\admin\controller;

use think\Controller;

class Base extends Controller
{
    protected $admin_id = 0;
    protected $admin_info = [];
    protected $is_founder = 0;//是否超管
    protected $admin_level = 1;//管理级别  1：超管 2：子账号 3：用户
    protected $menu_id;
    protected $request;


    protected function initialize()
    {
        $this->admin_info = session('admininfo');
        if (empty($this->admin_info)) {
            $this->error('亲，请先登录吧', url('common/login'));
        }
        session('admininfo', $this->admin_info);
        $this->admin_id = $this->admin_info['id'];
        $this->admin_level = $this->admin_info['level'];
        if (!$this->checkAuth($this->admin_info['role_id'])) {
            $this->error('亲，您无权限:' . $this->request->url(), url('common/login'));
        }
        $this->is_founder = $this->admin_info['founder'];
        $this->menu_id = $this->request->has('menuid') ? $this->request->param('menuid', 0, 'intval') : 0;
    }

    /**
     * 检查用户权限
     * @param $roleid  用户角色ID
     * @return bool
     */
    public function checkAuth($roleid, $url = '')
    {
        if ($this->admin_info['founder'] == 1) {
            return true;
        }
        if (empty($url)) {
            $url = strtolower($this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action());
        }
        return Auth::checkRule($url, $roleid);
    }

    /**
     * 获取用户组权限列表
     * @param $roleid
     * @return array|mixed
     */
    function getRole($roleid)
    {
        //用户的权限ID
        $auth_group = getCache('auth_group');
        if ($roleid == 1) {
            return $auth_group;
        }
        if (!isset($auth_group[$roleid]['rules'])) {
            return [];
        }
        return empty($auth_group[$roleid]['rules']) ? [] : explode(',', $auth_group[$roleid]['rules']);
    }

    /**
     * 判断权限
     * @param $roleid
     * @return bool
     */
    function checkMenuid($roleid)
    {
        return in_array($roleid, explode(',', $this->admin_info['rule']));
    }

}
